package com.shanks.home.module.ui;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shanks.home.R;
import com.shanks.home.model.UIData;

/**
 * ListView Adapter
 * @author Shanks
 * @time 2014.05.22
 */
public class UIListAdapter extends BaseAdapter {
	private ArrayList<UIData> arrayList;
	private Context context;
	private UIActivity uiActivity;
	
	public UIListAdapter(ArrayList<UIData> al, Context c, UIActivity ua) {
		arrayList = al;
		context = c;
		uiActivity = ua;
	}

	@Override
	public int getCount() {
		return arrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return arrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.ui_list_view_item, null);

			viewHolder = new ViewHolder();
			viewHolder.nameTV = (TextView)convertView.findViewById(R.id.ui_list_view_item_name_tv);
			
			convertView.setTag(viewHolder);
			
			convertView.setOnClickListener(onClickListener);
		} else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		UIData uiData = arrayList.get(position);
		viewHolder.id = uiData.id;
		viewHolder.nameTV.setText(uiData.name);

		return convertView;
	}	
	
	private class ViewHolder {
		public String id = "";
		public TextView nameTV;
	}
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			ViewHolder viewHolder = (ViewHolder)v.getTag();
			if (viewHolder.id == UIData.GRIDVIEW) {
				uiActivity.clickGridView();
			} else if (viewHolder.id == UIData.LISTVIEW) {
				
			} else if (viewHolder.id == UIData.SEEKBAR) {
				uiActivity.clickSeekBar();
			}
		}
	};
}